package com.hpq.common.event.listener;

import com.hpq.common.domain.enums.IdempotentEnum;
import com.hpq.common.domain.enums.UserActiveStatusEnum;
import com.hpq.common.event.UserOnlineEvent;
import com.hpq.common.event.UserRegisterEvent;
import com.hpq.user.dao.UserDAO;
import com.hpq.user.domain.enums.ItemEnum;
import com.hpq.user.domain.model.User;
import com.hpq.user.service.IpService;
import com.hpq.user.service.UserBackpackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 *  用户注册监听器
 */
@Component
@Slf4j
public class UserOnlineListener {
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private UserBackpackService userBackpackService;
    @Autowired
    private IpService ipService;
    @Async
    @TransactionalEventListener(value = UserOnlineEvent.class,phase = TransactionPhase.AFTER_COMMIT,fallbackExecution = true)
    public void saveUserIpInfo(UserOnlineEvent event){
        User user = event.getUser();
        User updateUser = new User();
        updateUser.setId(user.getId());
        updateUser.setIpInfo(user.getIpInfo());
        updateUser.setLastOptTime(user.getLastOptTime());
        updateUser.setActiveStatus(UserActiveStatusEnum.ONLINE.getType());
        userDAO.updateById(updateUser);
        // 用户ip详情解析
        ipService.refreshIpDetailAsync(user.getId());

    }
}
